package com.luck.emp.th.logic;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.luck.emp.common.SpOrderNoFour;
import com.luck.emp.common.SpWhOrderD;
import com.luck.emp.common.SpWhOrderM;
import com.luck.emp.entity.EmpTProdBase;
import com.luck.emp.entity.EmpTThD;
import com.luck.emp.entity.EmpTThM;
import com.luck.emp.th.qry.ThQry;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.dao.BaseJdbcDao;
import com.luck.pf.framework.logic.BaseLogic;
import com.luck.pf.util.CriteriaUtil;
import com.luck.pf.util.DateUtil;

/**
 * 说明
 * Copyright: Copyright (c) 2011
 * 深圳**科技
 * @author 
 * 
 */
public class ThLogic extends BaseLogic implements IThLogic{
	private BaseJdbcDao jdbcDao;
	

	
	public BaseJdbcDao getJdbcDao() {
		return jdbcDao;
	}

	public void setJdbcDao(BaseJdbcDao jdbcDao) {
		this.jdbcDao = jdbcDao;
	}

	public <T> T findThPage(ThQry qry, Class<T> cls) {
		Criteria criteria = baseDao.createCriteria(EmpTThM.class);
		CriteriaUtil.addEq(criteria, "thCode", qry.getThCode());
		CriteriaUtil.addFullLike(criteria, "contact", qry.getContact());
		criteria.add(Restrictions.between("createDate",
				DateUtil.getStartDateDay(qry.getStartDate()),
				DateUtil.getEndDateDay(qry.getEndDate())));
		Order order = Order.desc("thCode");
		criteria.addOrder(order);
		criteria.createAlias("dept", "d");
		filterData(criteria, qry.getUserInfo(), "d", null);
		if (cls.getName().equals("java.util.List")) {
			return (T) criteria.list();
		} else {
			return (T) baseDao.pagedQuery(criteria, qry.getPageNo(),qry.getPageSize());
		}
	}

	public List<EmpTThD> findThd(String thCode) {
		List<EmpTThD> ls = super.baseDao.find(" FROM EmpTThD WHERE thCode=?",thCode);
		return ls;
	}

	public String saveThAudit(UserBean userBean, EmpTThM entity,ThQry qry)
			throws Exception {
		StringBuffer bf = new StringBuffer();
		if(entity.getStatus().equals("2"))
			throw new Exception("<br>已经审核通过");
		else {
			entity.setAuditDate(DateUtil.getDate());
			entity.setAuditNote(qry.getNote());
			entity.setAuditor(userBean.getUser());
			entity.setModiDate(DateUtil.getDate());
			entity.setModiUser(userBean.getUser());
			entity.setStatus(qry.getStatus());
			if(qry.getStatus().equals("2")){//审核通过，则扣减库存
				//增加出入库记录
				String billType = "12";//采购退货单
				String ioType = "41";//采购退货出库
				SpWhOrderM sp = new SpWhOrderM(this.jdbcDao.getJdbcTemplate());
				String orderCode = sp.execute(billType, 20L, entity.getThCode(),qry.getWhCode(), entity.getVendorCode(), 
						null, null, null, qry.getNote(), userBean.getUserId(), ioType, null, entity.getCustId(), entity.getSaleId());
				//增加仓库订单子表
				List<EmpTThD> thds = findThd(entity.getThCode());
				for(EmpTThD thd:thds){
					thd.setModiUser(userBean.getUser());
					thd.setModiDate(DateUtil.getDate());
					SpWhOrderD spd = new SpWhOrderD(this.jdbcDao.getJdbcTemplate());
					spd.execute(orderCode, thd.getProductCode(), thd.getQty(), 0, 0, userBean.getUserId(), null);
				}
			}
			super.save(entity);
		}
		return null;
	}

	public String saveTh(UserBean userBean, EmpTThM entity,
			String[] productCodes, String[] priceReals, String[] qtys, Long[] ids)
			throws Exception {
		entity.setModiDate(DateUtil.getDate());
		entity.setModiUser(userBean.getUser());
		String thCode = entity.getThCode();
		entity.setStatus("1");
		if(StringUtils.isBlank(thCode)){//新建
			entity.setCreateDate(DateUtil.getDate());
			entity.setCreateUser(userBean.getUser());
			SpOrderNoFour sp = new SpOrderNoFour(this.jdbcDao.getJdbcTemplate());
			thCode = sp.execute(userBean.getDept().getDeptCode(), "CGTH");
			entity.setThCode(thCode);
			entity.setDeptId(userBean.getDeptId());
		}

		if(!entity.getStatus().equals("1"))
			throw new Exception("<br>此状态非新建，不能修改");
		super.save(entity);
		List<EmpTThD> dd = findThd(thCode);
		if(productCodes != null){
			for(int i=0; i < productCodes.length; i ++){
				EmpTProdBase base = super.get(EmpTProdBase.class, productCodes[i]);
				EmpTThD thd =null;
				if(ids[i] != null){
					thd = super.get(EmpTThD.class, ids[i]);
					if(dd.contains(thd))
						dd.remove(thd);
				}
				if(thd == null)
					thd = new EmpTThD();				
				thd.setCreateDate(DateUtil.getDate());
				thd.setCreateUser(userBean.getUser());
				thd.setModiDate(DateUtil.getDate());
				thd.setModiUser(userBean.getUser());
				thd.setHxStatus("1");//未核销
				thd.setThCode(thCode);
				thd.setProductCode(productCodes[i]);
				thd.setMeasUnit(base.getMeasUnit());
				thd.setPayAmt(0.00);
				thd.setPriceReal(Double.parseDouble(priceReals[i]));
				thd.setPurPrice(base.getSale().getPurPrice());
				thd.setQty(Long.parseLong(qtys[i]));
				super.save(thd);
			}
		}

		super.baseDao.getHibernateTemplate().deleteAll(dd);
		
		return null;
	}

	public String deleteTh(UserBean userBean, EmpTThM entity) throws Exception {
		if(!entity.getStatus().equals("1"))
			throw new Exception("<br>此状态非新建，不能删除");
		super.delete(entity);
		super.baseDao.getHibernateTemplate().deleteAll(findThd(entity.getThCode()));
		return null;
	}

}